package org.example.myleet.rosalind.rstr;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.calculate(90000, 0.6, "ATAGCCGA");
        solution.calculate(96691, 0.447518, "ATCGTCGTAT");
    }

    public void calculate(int n, double gcContent, String sequence) {
        int at = 0, cg = 0;
        for (int i = 0; i < sequence.length(); ++i) {
            char c = sequence.charAt(i);
            if (c == 'A' || c == 'T') {
                ++at;
            } else {
                ++cg;
            }
        }
        BigDecimal halfGcContent = BigDecimal.valueOf(gcContent).divide(BigDecimal.valueOf(2), RoundingMode.HALF_UP);
        BigDecimal halfAtContent = BigDecimal.valueOf(0.5).subtract(halfGcContent);
        BigDecimal occurOnce = BigDecimal.ONE;
        for (int i = 0; i < at; ++i) {
            occurOnce = occurOnce.multiply(halfAtContent);
        }
        for (int i = 0; i < cg; ++i) {
            occurOnce = occurOnce.multiply(halfGcContent);
        }
        BigDecimal none = BigDecimal.ONE.subtract(occurOnce).setScale(15, RoundingMode.HALF_UP);
        BigDecimal allNone = BigDecimal.ONE;
        for (int i = 0; i < n; ++i) {
            allNone = allNone.multiply(none);
        }
        BigDecimal atLeastOne = BigDecimal.ONE.subtract(allNone);
        System.out.println(atLeastOne.setScale(3, RoundingMode.HALF_UP));
    }
}
